package com.linkhub.oms.admin.modules.order.domain.vo;

import cn.hutool.core.collection.CollUtil;
import com.linkhub.oms.admin.modules.order.domain.vo.orderpdf.OrderPdfVO;
import com.linkhub.oms.common.util.QtStreamUtil;
import com.linkhub.oms.common.util.json.QtFastjsonUtil;
import com.linkhub.oms.entity.OrderEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @Description: OrderFulfillmentResultVO
 * @Author: echo
 * @Date: 2024/4/11 15:08
 * @Version: 1.0
 */
@Data
@ApiModel("订单履约结果对象")
public class OrderFulfillmentResultVO implements Serializable {

    @ApiModelProperty("成功数量")
    private int successTotal;

    @ApiModelProperty("失败数量，数量 > 0 才有失败原因")
    private int failTotal;

    @ApiModelProperty("失败原因")
    private String failReason;

    public static OrderFulfillmentResultVO createFailResult(String failReason, int failTotal) {
        OrderFulfillmentResultVO vo = new OrderFulfillmentResultVO();
        vo.setSuccessTotal(0);
        vo.setFailTotal(failTotal);
        vo.setFailReason(failReason);

        return vo;
    }

    public static OrderFulfillmentResultVO createResult(int successTotal, int failTotal, String failReason) {
        OrderFulfillmentResultVO vo = new OrderFulfillmentResultVO();
        vo.setSuccessTotal(successTotal);
        if (failTotal < 1) {
            return vo;
        }
        vo.setFailTotal(failTotal);
        vo.setFailReason(failReason);

        return vo;
    }

    public static OrderFulfillmentResultVO createResult(int total,
                                                        List<OrderEntity> emptyLogisticOrders,
                                                        List<OrderEntity> insufficientBalanceOrders,
                                                        List<OrderPdfVO> createFailPackagePdfs) {

        OrderFulfillmentResultVO vo = new OrderFulfillmentResultVO();
        StringBuilder failReason = new StringBuilder();
        if (CollUtil.isNotEmpty(emptyLogisticOrders)) {
            failReason.append(String.format("The logistics mode is empty, orderNumbers: [%s]\n",
                    QtFastjsonUtil.toJSONString(QtStreamUtil.map(emptyLogisticOrders, order -> String.valueOf(order.getOrderNumber())))));
        }

        if (CollUtil.isNotEmpty(insufficientBalanceOrders)) {
            failReason.append(String.format("Insufficient available balance, orderNumbers: [%s]",
                    QtFastjsonUtil.toJSONString(QtStreamUtil.map(insufficientBalanceOrders, order -> String.valueOf(order.getOrderNumber())))));
        }

        if (CollUtil.isNotEmpty(createFailPackagePdfs)) {
            failReason.append(String.format("The package pdf create or upload fail, orderNumbers: [%s]",
                    QtFastjsonUtil.toJSONString(QtStreamUtil.map(createFailPackagePdfs, orderPdf -> String.valueOf(orderPdf.getOrderNumber())))));
        }

        int failTotal = CollUtil.size(emptyLogisticOrders) + CollUtil.size(insufficientBalanceOrders) + CollUtil.size(createFailPackagePdfs);
        vo.setFailTotal(failTotal);

        if (failTotal > 0) {
            vo.setFailReason(failReason.toString());
        }

        vo.setSuccessTotal(total - failTotal);

        return vo;

    }
}
